// pages/trainDemand/trainDemand.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainPlan: {},
    inputDemand:'',
    demandList: [{
      name: '增肌',
      selected: false
    }, {
      name: '减脂',
      selected: false
    }, {
      name: '塑形',
      selected: false
    }, {
      name: '康复',
      selected: false
    }, {
      name: '体能',
      selected: false
    }, {
      name: '保持健康',
      selected: false
    }, {
      name: '调整体态',
      selected: false
    }, {
      name: '娱乐放松',
      selected: false
    }, {
      name: '瘦腰',
      selected: false
    }, {
      name: '瘦手臂',
      selected: false
    }, {
      name: '瘦大腿',
      selected: false
    }, {
      name: '练翘臀',
      selected: false
    }, {
      name: '追求腹肌',
      selected: false
    }, {
      name: '改善柔韧性',
      selected: false
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const trainPlanJson = wx.getStorageSync("trainPlan");
    const trainPlan = JSON.parse(trainPlanJson);
    const demandList = this.data.demandList;
    demandList.forEach(function(item){
      if (trainPlan.trainDemand && trainPlan.trainDemand.indexOf(item.name)>=0){
        item.selected = true;
      }
    });
    this.setData({
      trainPlan: trainPlan,
      demandList: demandList
    });
  },
  changeSelect:function(e){
    const index = e.currentTarget.dataset.index;
    const demandList = this.data.demandList;
    demandList[index].selected = !demandList[index].selected;
    this.setData({
      demandList: demandList
    });
  },
  inputDemand:function(e){
    const value = e.detail.value;
    this.setData({ inputDemand: value});
  },
  addDemand:function(){
    const demandList = this.data.demandList;
    const inputDemand = this.data.inputDemand;
    if (inputDemand.length>0){
      demandList.push({
        name: inputDemand,
        selected: true
      });
      this.setData({
        demandList: demandList,
        inputDemand: ''
      });
    }
  },
  nextPage:function(){
    const selectOptions = [];
    const demandList = this.data.demandList;
    demandList.forEach(function(item){
      if(item.selected){
        selectOptions.push(item.name);
      }
    });
    const trainPlan = this.data.trainPlan;
    wx.setStorageSync("selectDemands", JSON.stringify(selectOptions));
    wx.setStorageSync("remark", trainPlan.remark);
    wx.navigateTo({
      url: '/pages/trainDemand2/trainDemand2',
    });
  }

})